package com.zizo.autohome.sta.competition.price;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

import com.zizo.util.ConnectionPool;

public class CompPriceDao implements Runnable {
	private Connection conn;
	private int cid;
	private String cname;
	private double price;
	private int year;

	public CompPriceDao(Connection conn, int cid, String cname, double price, int year) {
		super();
		this.conn = conn;
		this.cid = cid;
		this.cname = cname;
		this.price = price;
		this.year = year;
	}

	public List<CompPriceDao> getBrand() {
		List<CompPriceDao> list = new ArrayList<>();
		Statement st = null;
		ResultSet rs = null;
		String sql = "SELECT brand_id,brand_name,brand_price,brand_price_year FROM carvoice_biz.biz_brand WHERE brand_price is not null;";
		try {
			st = conn.createStatement();
			rs = st.executeQuery(sql);
			while (rs.next()) {
				int bid = rs.getInt(1);
				String bname = rs.getString(2);
				double price = rs.getDouble(3);
				int year = rs.getInt(4);
				CompPriceDao c = new CompPriceDao(conn, bid, bname, price, year);
				list.add(c);
			}
		} catch (SQLException e) {
			e.printStackTrace();
		}
		return list;
	}

	public void update() {
		Statement st = null;
		ResultSet rs = null;
		String sql = "SELECT brand_id,brand_name,brand_price,ABS(brand_price-" + price
				+ ") AS diff,brand_price_year FROM carvoice_biz.biz_brand WHERE brand_price is not null and brand_id !="
				+ cid + " and brand_price_year in (YEAR(NOW())-1,YEAR(NOW())) ORDER BY diff ASC LIMIT 20;";
		try {
			st = conn.createStatement();
			rs = st.executeQuery(sql);
			while (rs.next()) {
				int brand_id = rs.getInt(1);
				String brand_name = rs.getString(2);
				double brand_price = rs.getDouble(3);
				double diff = rs.getDouble(4);
				int brand_year = rs.getInt(5);
				CompPrice compprice = new CompPrice(cid, brand_id, cname, brand_name, price, brand_price, diff, year,
						brand_year);
				insert(compprice);
			}
		} catch (SQLException e) {
			e.printStackTrace();
		}
	}

	public void insert(CompPrice com) {
		PreparedStatement ps = null;
		String sql = "INSERT INTO carvoice_biz.biz_competition_price(cp_cid1,cp_cname1,cp_cid2,cp_cname2,cp_price1,cp_price2,cp_year1,cp_year2,cp_diff) values(?,?,?,?,?,?,?,?,?)";
		try {
			ps = conn.prepareStatement(sql);
			ps.setInt(1, com.getCid1());
			ps.setString(2, com.getCname1());
			ps.setInt(3, com.getCid2());
			ps.setString(4, com.getCname2());
			ps.setDouble(5, com.getPrice1());
			ps.setDouble(6, com.getPrice2());
			ps.setInt(7, com.getYear1());
			ps.setInt(8, com.getYear2());
			ps.setDouble(9, com.getPrice_diff());
			ps.executeUpdate();
		} catch (SQLException e) {
			e.printStackTrace();
		}
	}

	public CompPriceDao(Connection conn) {
		super();
		this.conn = conn;
	}

	public Connection getConn() {
		return conn;
	}

	public void setConn(Connection conn) {
		this.conn = conn;
	}

	public void run() {
		update();
		System.out.println(cid);
	}

	public static void main(String[] args) {
		Connection conn = ConnectionPool.getConnection();
		CompPriceDao comp = new CompPriceDao(conn);
		List<CompPriceDao> list = comp.getBrand();
		ExecutorService fix = Executors.newFixedThreadPool(20);
		for (CompPriceDao dao : list) {
			dao.setConn(conn);
			fix.execute(dao);
		}
		fix.shutdown();
	}
}
